package com.pacy.swj.swjwb.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.pacy.swj.swjwb.MainActivity;
import com.pacy.swj.swjwb.R;
import com.pacy.swj.swjwb.Utils.Constans;
import com.pacy.swj.swjwb.Utils.HttpUtils;
import com.pacy.swj.swjwb.beans.BaseBean;
import com.pacy.swj.swjwb.beans.QureyMapBean;
import com.pacy.swj.swjwb.beans.SearchBean;
import com.tencent.smtt.sdk.WebView;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import h.framework.android.utils.JsonUtil;
import h.framework.android.utils.LogUtil;
import h.framework.android.utils.ToastUtil;

public class WebActivity extends AppCompatActivity {
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.right_tv)
    TextView right_tv;
    @BindView(R.id.web)
    WebView webView;
    private long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).init();
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleTv.setText(getIntent().getStringExtra("title"));
        right_tv.setText(getIntent().getStringExtra("right"));
        id = getIntent().getLongExtra("id", 0);
        sendRequest();
        initWebView();
    }

    private void initWebView() {
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.getSettings().setBlockNetworkImage(false);
    }


    /**
     * 请求具体内容
     */
    private void sendRequest() {
        Map<String, String> map = new HashMap<>();
        map.put("id", id + "");
        map.put("current", "1");
        map.put("size", "1");
        HttpUtils.PostForm(Constans.SEARCH, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.i(result);
                BaseBean<SearchBean> baseBean = JsonUtil.jsonToObject(result, new TypeToken<BaseBean<SearchBean>>() {
                }.getType());
                if (baseBean.isSuccess()) {
                    if (baseBean.getData() != null) {
                        if (baseBean.getData().getRecords().size() > 0) {
                            webView.loadDataWithBaseURL(null, baseBean.getData().getRecords().get(0).getContent(), "text/html", "UTF-8", null);
                        }
                    }
                } else {
                    ToastUtil.error((String) baseBean.getMessage());
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.i(ex.toString());
                ToastUtil.error("请求出错，请重试");
                finish();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @OnClick(value = {R.id.back_iv,R.id.right_tv})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.back_iv:
                finish();
                break;

            case R.id.right_tv:
                Intent intent=new Intent(WebActivity.this,MainActivity.class);
                startActivity(intent);
                break;
        }
    }
}
